package ee.ttu.karileet.serviceshop.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import ee.ttu.karileet.serviceshop.bo.CustomerBO;
import ee.ttu.karileet.serviceshop.dao.CustomerDAO;
import ee.ttu.karileet.serviceshop.dao.PersonDAO;
import ee.ttu.karileet.serviceshop.dao.SubjectTypeDAO;
import ee.ttu.karileet.serviceshop.model.Customer;
import ee.ttu.karileet.serviceshop.model.Person;
import ee.ttu.karileet.serviceshop.model.SubjectType;
import ee.ttu.karileet.serviceshop.service.CustomerService;


@Service("customerService")
public class CustomerServiceImpl implements CustomerService {

	@Autowired
	private CustomerDAO customerDao;
	@Autowired
	private PersonDAO personDao;
	@Autowired
	private SubjectTypeDAO subjectTypeDao;
	
	@Override
	@Transactional
	public void insertCustomer(CustomerBO object) {
//		customerDao.insertCustomer(object);
		//TODO: IMPLEMENT
	}

	@Override
	@Transactional
	public List<CustomerBO> getCustomers() {
		List<Customer> customers = customerDao.getCustomers();
		List<CustomerBO> bos = new ArrayList<CustomerBO>();
		for (Customer c : customers) {
			bos.add(getCustomerById(c.getCustomer()));
		}
		return bos;
	}

	@Override
	@Transactional
	public CustomerBO getCustomerById(long id) {
		Customer c = customerDao.getCustomerById(id);
		CustomerBO cust = new CustomerBO();
		cust.setCustomer(c.getCustomer());
		Person subject = personDao.getPersonById(c.getSubjectFk());
		cust.setSubject(subject);
		SubjectType subjectType = subjectTypeDao.getSubjectTypeById(c.getSubjectTypeFk());
		cust.setSubjectType(subjectType);
		return cust;
	}
	

}